import Subject from "./subject";

/**
 * 
 */

let _cloneImpl = (obj) => {
    return JSON.parse(JSON.stringify(obj));
}

export default class SyncState2 extends Subject {
    protected _origin: any = {};
    protected _prev: any = {};
    protected _cur: any = {};

    toJSON() {
        return {};
    }
    fromJSON() {

    }

    assign(kv: any) {
        let data = _cloneImpl(kv);
        this._cur = {};
        this._prev = {};
        Object.assign(this._cur, data);

        this._origin = {};
        data = _cloneImpl(kv);
        Object.assign(this._origin, data);
    }

    resetState(...keys: string[]) {
        let state = this.start();
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            state[key] = this._origin[key];
        }
        this.end();
    }

    equal(key) {
        return globalThis._.isEqual(this._prev[key], this._cur[key])
    }
    hasChange(key: string) {
        return !this.equal(key);
    }

    //与，查看新旧状态的变化，KEYS是字段。
    hasChangeAnd(...keys: string[]) {
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            if (this.equal(key)) {
                return false;
            }
        }
        return true;
    }
    //或，查看新旧状态的变化，KEYS是字段。
    hasChangeOr(...keys: string[]) {
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            if (!this.equal(key)) {
                return true;
            }
        }
        return false;
    }

    // set(key, value) {
    //     if (!this.has(key)) {
    //         console.log(`SyncState has no ${key}`)
    //         return;
    //     }
    //     this._temp[key] = value;
    // }
    // inc(key, value = 1) {
    //     if (!this.has(key)) {
    //         console.log(`SyncState has no ${key}`)
    //         return;
    //     }
    //     this._temp[key] += value;
    // }
    // has(key): boolean {
    //     return this._temp.hasOwnProperty(key);
    // }
    // get(key) {
    //     if (!this.has(key)) {
    //         console.log(`SyncState has no ${key}`)
    //         return;
    //     }
    //     return this._temp[key];
    // }
};


class SyncState {
    _origin: Map<string, any>;
    _prev: Map<string, any>;
    _cur: Map<string, any>;
    _temp: Map<string, any>;

    has(key): boolean {
        return true;
    }
    assign(kv: Object) {
        console.assert(!!kv, `SyncState::assign`);
        for (var key in kv) {
            this.map.set(key, kv[key]);
        }
    }
    batch()
    set(key, val) {
        console.assert(this.has(key), `SyncState::set 没有找到${key}`);
        this.map.set( key, val );
        return this;
    }
    get(key) {
        console.assert(this.has(key), `SyncState::get 没有找到${key}`);
        return this.map.get(key);
    }

    equal( key, val ): boolean {
        return true;
    }
    hasChange(...keys: string[]): boolean {
        return true;
    }
    hasChangeAnd(...keys: string[]): boolean {
        return true;
    }
    hasChangeOr(...keys: string[]): boolean {
        return true;
    }
    merge() { }
};




//初始化
let syncState = new SyncState();
syncState.assign({
    movement: "idle",
    k1: [ [], [] ],
    k2: [0, 0, 0],
    k3: [1, 2, 3],
    count: 0,
});


//写数据
onClick() {
    syncState.set("movement", "clicked"); //movement==clicked
    syncState.merge(); //结束，同步。

updateUi(... ) {
    if (syncState.equal("k2", [2, 3, 4])) {
        //干点事
    }

    //读数据：
    if (syncState.hasChange("k2")) {
        //更新ui
        let k2 = syncState.get("k2");
    }

    if (syncState.hasChangeAnd("k2", "k3")) {
        //更新ui
    }

    if (syncState.hasChangeAnd("count", "k2", "k3")) {
        //更新ui
    }

    if (syncState.hasChangeOr("movement", "k3")) {
        //更新ui
    }
